/**
 * Created by shenhai on 15/9/4.
 */

angular.module('backofficeApp.userManager.controllers', [])
    .controller('userCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {
        //gObj.getFile("/public/js/common/loadResource.js");
        //初始化左右选择框
        var larSelect = new gObj.larSelect();
        larSelect.paint($(".ral-list-choice"));


        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/backoffice/user/userList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.userList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.userList = null;
            },
            params: {}
        });

        $scope.search = function() {

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            var body = {};
            if ($scope.desc) body.loginName = $scope.desc;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/userList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                $scope.userList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;

            }).error(function () {
                $scope.userList = null;
            });
        };

        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };

        //删除
        $scope.delete = function(){
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/user/userDelete',
                checkObj: $(".item-check"),//checkbox对象
                key:"userID",
                success: function(data){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);
                },
                error: function(err){}
            };
            $scope.pagination.listDelete(params);
        };

        //新增
        $scope.add = function(addData){
            addData.roleID = $(".lists_selected").val();
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/user/userAdd',
                addData: addData,
                success: function(data){
                    $scope.pagination.selectPage(1);//插入成功显示第一页
                    $('#doc-modal-add').modal("close");
                },
                error: function(err){
                    alert(err);
                }
            };
            $scope.pagination.listAdd(params);
        };
        //编辑
        $scope.update = function(updateData,temp){
            var obj = temp.target;
            var uData = {userID:"",user:{}};
            if(updateData.sysUserID)uData.userID = Number(updateData.sysUserID);
            if(updateData.nickName)uData.user.nickName = updateData.nickName;
            if(updateData.isValid)uData.user.isValid = Number(updateData.isValid);
            if(updateData.remark)uData.user.remark = updateData.remark;
            uData.user.roleID = $scope.getSelectedRole($(obj.parentNode.parentNode).find(".lists_selected option"));
            //uData = deleteObjNull(uData);
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/user/userEdit',
                updateData: uData,
                success: function(data){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);

                    if(data.result.status == 1){
                        $('#doc-modal-'+(number-1)).modal("close");
                        alert("修改成功！");
                    }else {
                        $('#doc-modal-'+(number-1)).modal("close");
                        alert("修改失败！");
                    }
                },
                error: function(){
                    $('#doc-modal-'+(number-1)).modal("close");
                    alert("修改失败！");
                }
            };
            $scope.pagination.listUpdate(params);
        };

        //获取权限列表
        var roleList = function(){
            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/role/roleList', {}, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.roleList = data.data;
                larSelect.addRole($scope.roleList);
            }).error(function (err) {
                alert(err);
            });
        };
        roleList();

        $scope.getEditRole = function(data,index){
            larSelect.paint($("#ral-list-choice"+index));
            larSelect.editRole($scope.roleList,data);
        };
        $scope.getSelectedRole = function(data){
            var array = [];
            for(var i = 0;i < data.length;i++){
                array.push(data[i].value);
            }
            return array;
        }
    }]).controller('changePasswordCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {


        $scope.update = function(updateData){

            if(!updateData){return}

            if(!updateData.old){
                alert('请输入原密码！')
                return;
            };

            if(!updateData.new){
                alert('请输入新密码！')
                return;
            };

            if(updateData.new != updateData.renew){
                alert('两次输入的新密码不一致！')
                return;
            }

            var opt = {
                oldpassWord : updateData.old,
                passWord : updateData.new
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/user/changePassword', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(data.result.status == 0){
                    alert(data.result.message);
                    return;
                }else{
                    $scope.updateData = {};
                    alert('修改成功！')
                };

            }).error(function (err) {
                alert(err);
            });
        };

    }]);